﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class LateralConnOperation : ConnOperation
	{
		public LateralConnOperation(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public override bool SelectDucts()
		{
			bool flag = false;
			if (this.SelectDuctsMethod != null)
			{
				string errorString = "";
				IList<Duct> source = null;
				IList<XYZ> source2 = null;
				flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.TakeOffConn, ref source, ref source2, ref errorString);
				if (flag)
				{
					base.SelectedDucts = source.ToList<Duct>();
					base.SelectedPoints = source2.ToList<XYZ>();
				}
				else
				{
					base.ErrorString = errorString;
				}
			}
			return flag;
		}

		public override bool Connect()
		{
			bool result = false;
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "选择的风管个数不对！";
				return result;
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (!connectDuctManage.BeSameShapeDucts(base.SelectedDucts))
			{
				base.ErrorString = "选择的风管截面必须一致！";
				return result;
			}
			int ductShape = connectDuctManage.GetDuctShape(base.SelectedDucts[0]);
			FamilySymbol familySymbol = null;
			Element element = null;
			string empty = string.Empty;
			if (base.ConnForm.Flag)
			{
                int numberOfRules = base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetNumberOfRules((RoutingPreferenceRuleGroupType)2);
				for (int i = 0; i < numberOfRules; i++)
				{
                    if (base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId != null)
					{
                        element = base.UIDoc.Document.GetElementById(base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId);
					}
					if (element != null)
					{
						UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
						FamilyInstance familyInstance = base.UIDoc.Document.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, element, base.UIDoc.Document.ActiveView.GenLevel, 0);
						
                        
                        if ((int)(familyInstance.MEPModel as MechanicalFitting).PartType == 11 ||
                            (int)(familyInstance.MEPModel as MechanicalFitting).PartType == 10)
						{
							familySymbol = (element as FamilySymbol);
							base.UIDoc.Document.Delete(familyInstance.Id);
							break;
						}

						base.UIDoc.Document.Delete(familyInstance.Id);
					}
				}
				if (familySymbol == null)
				{
					base.ErrorString = "没有可以使用的族配置，请检查Revit的族配置";
					return result;
				}
			}
			else
			{
				connectDuctManage = new ConnectDuctManage();
				if (ductShape == 1)
				{
					this.fittingStyle = ConnectDuctStyle.Side;
				}
				else if (ductShape == 0)
				{
					this.fittingStyle = ConnectDuctStyle.SideRound;
				}
				List<string> fittingNames = connectDuctManage.GetFittingNames(this.fittingStyle);
				if (fittingNames.Count != 1)
				{
					base.ErrorString = "获取侧连接族名称失败！";
					return result;
				}
				familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], base.UIDoc.Document, ref empty);
			}
			FamilyInstance familyInstance2 = hyductConnect.NewTakeOffFitting(base.SelectedDucts, base.SelectedPoints, familySymbol, ref empty);
			if (familyInstance2 != null && familySymbol != null)
			{
				familyInstance2.Symbol = familySymbol;
				base.UIDoc.Document.Regenerate();
				result = true;
			}
			if (familySymbol == null)
			{
				base.ErrorString = empty;
				base.UIDoc.Document.Regenerate();
			}
			base.ErrorString = empty;
			return result;
		}

		public override void GetRuleList()
		{
			this.RuleList = UnifiedModified.GetSegmentRuleFitting(base.SelectedDucts.First<Duct>().DuctType, 6);
		}

		public override void SetRuleList()
		{
			if (this.RuleList != null)
			{
				UnifiedModified.SetSegmentRuleFitting(base.SelectedDucts.First<Duct>().DuctType, 6, this.RuleList);
			}
		}
	}
}
